package com.ship.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ship.common.core.base.BaseController;
import com.ship.common.core.base.Page;
import com.ship.common.core.validate.Add;
import com.ship.common.core.validate.Update;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.contract.bean.ConTemplate;
import com.ship.contract.bean.ConTemplateClause;
import com.ship.contract.service.ConTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 31463
 */
@RestController
@RequestMapping("/conTemplate")
public class ConTemplateController extends BaseController {

    @Autowired
    private ConTemplateService conTemplateService;

    /**
     * 分页
     *
     * @param conTemplate
     * @return
     */
    @PostMapping("/page")
    public JsonResult<PageInfo<ConTemplate>> page(@RequestBody ConTemplate conTemplate) {
        PageInfo<ConTemplate> pageInfo = conTemplateService.queryPage(conTemplate);
//        List<ConTemplate> exportList = page.getList();
//
//        // 对列表按时间倒序排序
//        Collections.sort(exportList, (e1, e2) -> e2.getCreateDate().compareTo(e1.getCreateDate()));
//
//        long pageSize = page.getPageSize();
//        long currentPage = page.getPageNum();
//        int startOrderNumber = (int) ((currentPage - 1) * pageSize + 1);
//
//        // 为每个Export对象设置orderNumber
//        for (int i = 0; i < exportList.size(); i++) {
//            exportList.get(i).setOrderNumber(startOrderNumber + i);
//        }
//
//        page.setList(exportList);
        return JsonResult.success(pageInfo);
    }

    /**
     * 新增
     *
     * @param conTemplate
     * @return
     */
    @PostMapping
    @Log(title = "新增合同模板",businessType = BusinessType.INSERT)
    public JsonResult<Object> add(@RequestBody @Validated(Add.class)ConTemplate conTemplate) {
        conTemplateService.saveCon(conTemplate);
        return JsonResult.success();
    }

    /**
     * 修改
     *
     * @param conTemplate
     * @return
     */
    @PutMapping
    @Log(title = "修改合同模板",businessType = BusinessType.UPDATE)
    public JsonResult<Object> update(@RequestBody @Validated(Update.class)ConTemplate conTemplate) {
        conTemplateService.updateById(conTemplate);
        return JsonResult.success();
    }

    /**
     * 删除申请
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除合同模板",businessType = BusinessType.DELETE)
    public JsonResult delete(@PathVariable Long id){
        conTemplateService.removeById(id);
        conTemplateService.removeByTemplateId(id);
        return JsonResult.success();
    }

    /**
     * 详情
     *
     * @return
     */
    @GetMapping
    public JsonResult<Map<String,Object>> detail(Long id) {
        return JsonResult.success(conTemplateService.detail(id));
    }

    /**
     * 保存模板条款
     * @param conTemplateClause
     * @return
     */
    @PostMapping("/saveClause")
    public JsonResult<Object> saveClause(@RequestBody ConTemplateClause conTemplateClause){
        conTemplateService.saveClause(conTemplateClause);
        return JsonResult.success();
    }

    /**
     * 修改模板条款
     * @param conTemplateClause
     * @return
     */
    @PutMapping("/putClause")
    public JsonResult<Object> putClause(@RequestBody ConTemplateClause conTemplateClause){
        conTemplateService.putClause(conTemplateClause);
        return JsonResult.success();
    }

    /**
     * 删除模板条款
     * @param id
     * @return
     */
    @DeleteMapping("/delClause/{id}")
    public JsonResult<Object> delClause(@PathVariable Long id){
        conTemplateService.delClause(id);
        return JsonResult.success();
    }

    /**
     * 查询合同模板所有条款类别
     *
     * @return
     */
    @GetMapping("/templateClauseTypes")
    public JsonResult<List<Map<String,Object>>> templateClauseTypes(Long id) {
        return JsonResult.success(conTemplateService.templateClauseTypes(id));
    }
}
